// pages/course/voiceCourse/index/index.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navList: [],
        courseList: [],
        navType: 0,
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getVoiceCourseNav()
        this.getCourseList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            navList: [],
            courseList: [],
            navType: 0,
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getCourseList()
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/course/voiceCourse/detail/detail?courseId=' + e.currentTarget.dataset.id
        })
    },

    selectNav: function(e) {
        if (this.data.navType == e.currentTarget.dataset.id) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            navType: e.currentTarget.dataset.id,
            courseList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getCourseList()
    },

    getVoiceCourseNav: function() {
        const that = this
        let navList = this.data.navList
        let data = {
            activity: 785
        }
        common.post('Online/online_class', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    navList: navList,
                })
            } else {
                navList = navList.concat(res)
                that.setData({
                    navList: navList
                })
            }
        }).catch(function(e) {})
    },

    getCourseList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let courseList = this.data.courseList
        let data
        if (this.data.navType == 0) {
            data = {
                activity: 785,
                page: this.data.nowPage,
                limit: 10
            }
        } else {
            data = {
                activity: 785,
                online_class_id: this.data.navType,
                page: this.data.nowPage,
                limit: 10
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Online/online_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    courseList: courseList,
                    isAll: true
                })
            } else {
                courseList = courseList.concat(res)
                that.setData({
                    courseList: courseList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    }
})